package com.small.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.basis.result.Result;
import com.common.basis.result.ResultPage;
import com.common.basis.result.ResultSet;
import com.small.account.entity.AuRole;
import com.small.account.entity.RoleAndPermission;
import com.small.account.entity.vo.AuRoleQ;
import com.small.account.entity.vo.RoleAuthorize;
import com.small.account.service.IAuRoleService;
import com.small.account.service.IRoleAndPermissionService;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 角色
 *
 * @author zhengboda
 * @date 2020-01-08 14:37
 **/
@RestController
@AllArgsConstructor
@RequestMapping({"/au-role"})
public class AuRoleController {
    private final IAuRoleService iAuRoleService;
    private final IRoleAndPermissionService iRoleAndPermissionService;


    /**
     * 获取
     * @return
     */
    @GetMapping
    public ResultPage<AuRole> findTree(AuRoleQ param) {
        LambdaQueryWrapper<AuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AuRole::getCreated)
                .like(param.getRoleName() != null, AuRole::getRoleName, param.getRoleName());
        Page<AuRole> page = iAuRoleService.page(param.page(), wrapper);
        return ResultSet.page(page);
    }

    /**
     * 新增
     */
    @PostMapping()
    public Result<?> post(@RequestBody @Validated AuRole param) {
        LambdaQueryWrapper<AuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AuRole::getRoleName, param.getRoleName());
        int count = iAuRoleService.count(wrapper);
        if (count > 0) {
            return ResultSet.error("编号重复");
        }
        boolean b = iAuRoleService.save(param);
        return ResultSet.operation(b);
    }

    /**
     * 修改
     */
    @PutMapping()
    public Result<?> put(@RequestBody @Validated AuRole param) {
        LambdaQueryWrapper<AuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(AuRole::getId, param.getId());
        wrapper.eq(AuRole::getRoleName, param.getRoleName());
        int count = iAuRoleService.count(wrapper);
        if (count > 0) {
            return ResultSet.error("编号重复");
        }
        boolean b = iAuRoleService.updateById(param);
        return ResultSet.operation(b);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @Transactional(rollbackFor = Exception.class)
    public Result<?> del(@RequestParam String id) {
        boolean b = iAuRoleService.removeById(id);
        LambdaQueryWrapper<RoleAndPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleAndPermission::getRoleId, id);
        iRoleAndPermissionService.remove(wrapper);
        return ResultSet.operation(b);
    }

    /**
     * 授权
     */
    @PutMapping("authorize")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> authorize(@RequestBody @Validated RoleAuthorize param) {
        String id = param.getId();
        List<String> list = param.getList();
        ArrayList<RoleAndPermission> roleAndPermissions = new ArrayList<>();
        for (String s : list) {
            RoleAndPermission roleAndPermission = new RoleAndPermission();
            roleAndPermission.setPermission(s);
            roleAndPermission.setRoleId(id);
            roleAndPermissions.add(roleAndPermission);
        }
        LambdaQueryWrapper<RoleAndPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleAndPermission::getRoleId, id);
        iRoleAndPermissionService.remove(wrapper);
        boolean b = iRoleAndPermissionService.saveBatch(roleAndPermissions);
        return ResultSet.operation(b);
    }
}
